<?php

// No direct access
defined('_JEXEC') or die;
jimport( 'joomla.user.authentication' );


/**
 * Basic anonymous connection for XMLRPC, JSON and SOAP
 * 
 * @since		1.7
 */


class AnonymousAccess
{

	public static function test($method_name, $params, $app_data)
	{
		return "Connection successful, welcome to AnJoLinA Anonymous";
	}
	
	public static function getReleasedContent($method_name, $params, $app_data)
	{
		$AJLparams = &JComponentHelper::getParams('com_anjolina'); 
		$debug = $AJLparams->get('debugmode');
		
		if($debug=="1"||$debug=="2"){
			// Add the logger.
			JLog::addLogger(
				array(
		            // Set the name of the log file
		            'text_file' => 'com_anjolina.log.php',
		            // (optional) you can change the directory
		            'text_file_path' => 'logs/anjolina'
			     )
			);
		}
		
		$granted_access = 1;  //Guest Access

		$data = array();
		$textdata = array();
		$media = array();
			
		if($granted_access > -1){
			$db = JFactory::getDbo();
			$query = $db->getQuery(true);
			$sqlstring = "SELECT
								c.id,
								c.title,
								c.catid,
								c.introtext,
								c.access,
								c.fulltext
								FROM
								`#__anjolina` AS a
								Inner Join `#__content` AS c ON a.catid = c.catid".
								" WHERE c.access <= ".$granted_access;
			
			$db->setQuery($sqlstring);
			
			if($db->query()){
				$rows = $db->loadAssocList();
						
				foreach($rows as $key => $value){
						
					$textdata[] = array("id" => $value['id'],
								"title" => Utils::clearHTML($value['title']),
								"introtext" => Utils::extractMedia($value['introtext'], $media),
								"fulltext" => Utils::extractMedia($value['fulltext'], $media),
								"parent" => $value['catid'],
								"access" => $value['access']);			
					if($debug=="2")
						JLog::add("adding content: ".$value['title'], JLog::DEBUG, 'AnJoLinA Anonymous');
				}
			}
			else{
				//Error
			}	
			
			$data[] = $textdata;
			$data[] = $media;
		}
		else{
			//invalid
		}
		
		return $data;
	}
	
	public static function getReleasedCategories($method_name, $params, $app_data)
	{
		$AJLparams = &JComponentHelper::getParams('com_anjolina'); 
		$debug = $AJLparams->get('debugmode');
		
		if($debug=="1"||$debug=="2"){
			// Add the logger.
			JLog::addLogger(
				array(
		            // Set the name of the log file
		            'text_file' => 'com_anjolina.log.php',
		            // (optional) you can change the directory
		            'text_file_path' => 'logs/anjolina'
			     )
			);
		}
		
		$granted_access = 1; //Guest Access
		
		if($granted_access > -1){
			$data = array();
			$db = JFactory::getDbo();
			$query = $db->getQuery(true);
	
			$sqlstring = "SELECT
							c.id,
							c.title,
							c.description,
							c.level,
							c.parent_id,
							c.access
							FROM
							`#__anjolina` AS a
							Inner Join `#__categories` AS c ON a.catid = c.id".
							" WHERE c.access <= ".$granted_access;
			
			$db->setQuery($sqlstring);
			
			if($db->query()){
				$rows = $db->loadAssocList();
	
				foreach($rows as $key => $value){
									
					$data[] = array("id" => $value['id'],
									"title" => Utils::clearHTML($value['title']),
									"description" => Utils::clearHTML($value['description']),
									"parent" => $value['parent_id'],
									"level" => $value['level'],
									"access" => $value['access'],
									"media" => Utils::getMedia($value['description']));												
				}
			}
			else{
				//Error
			}	
		}
		return $data;
	}
	
	
	public static function getAll()
	{
		$textdata[] = array(
							"categories" => AnonymousAccess::getAnonReleasedCategories(),
							"content" => AnonymousAccess::getAnonReleasedContent()
							);
		
		return $textdata;
	}
	
	
}
